##Collect the s.d. of xi and model - level xi
set.seed(seedn)
data.exog.mm <- DGP.BLP(year.chosen=year.monte,vdgp = 3) #gen exog data
BLP.output <- rich.subs.BLP.mlog(dat=data.exog.mm,verbose=FALSE,objective=objective) 
BLP.output.m <- BLP.output[m==j]
BLP.meanownelas <- BLP.output.m[, mean(own_elas_m)]
BLP.priceadjust <- BLP.output.m[, mean(p_m * (1-s_m))] #  v=1 version --> wvp_m = 0 --> average own elasticity will be same as v=3

#output xi and x in same file
xandxi.v3 <- data.table(vxi=3,BLP.output.m[,.(m,hpwt=hpwt_m,air=air_m,mpd=mpd_m,space=space_m,xi=xi.predicted_m,mc=mc_m)])
#output sd of xi
sd.xi.v3 <- BLP.output.m[, sd(xi.predicted_m)]
#
#
#output sd.xi with different versions
set.seed(seedn)
data.exog.mm <- DGP.BLP(year.chosen=year.monte,vdgp = 2) #gen exog data
BLP.output <- rich.subs.BLP.mlog(dat=data.exog.mm,verbose=FALSE,objective=objective) 
BLP.output.m <- BLP.output[m==j]
xandxi.v2 <- data.table(vxi=2,BLP.output.m[,.(m,hpwt=hpwt_m,air=air_m,mpd=mpd_m,space=space_m,xi=xi.predicted_m,mc=mc_m)])
sd.xi.v2 <-   BLP.output.m[, sd(xi.predicted_m)]
#
set.seed(seedn)
data.exog.mm <- DGP.BLP(year.chosen=year.monte,vdgp = 1) #gen exog data
BLP.output <- rich.subs.BLP.mlog(dat=data.exog.mm,verbose=FALSE,objective=objective) 
BLP.output.m <- BLP.output[m==j]
xandxi.v1 <- data.table(vxi=1,BLP.output.m[,.(m,hpwt=hpwt_m,air=air_m,mpd=mpd_m,space=space_m,xi=xi.predicted_m,mc=mc_m)])
sd.xi.v1 <- BLP.output.m[, sd(xi.predicted_m)]
#
sd.xi <- data.table(v=1:3,sd.xi=c(sd.xi.v1,sd.xi.v2,sd.xi.v3) )
fwrite(sd.xi,"Tables/CF_BLPdata/BLPsd_xi.csv")
sd.xi
#
xandxi <- rbind(xandxi.v1,xandxi.v2,xandxi.v3) 
fwrite(xandxi,paste0("Tables/CF_BLPdata/BLP_xandxi1990_",n.hh,"hh_",sequences,".csv"))
#
xandxi[,.(meancost=mean(mc)),by=vxi]
